﻿using ErrorOr;
using Gnivc.CarDealership.Api.Application.Abstractions.Repositories;
using Gnivc.CarDealership.Api.Domain.Entities;
using Gnivc.CarDealership.Api.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;

namespace Gnivc.CarDealership.Api.Infrastructure.Repositories;

public class UserRepository : IUserRepository
{
    private readonly IApplicationDbContext _context;

    public UserRepository(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<ErrorOr<User>> GetByIdAsync(Guid id)
    {
        var user = await _context.Users
            .Where(u => !u.IsDeleted && u.Id == id)
            .SingleOrDefaultAsync();

        if (user == null)
            return Error.NotFound();

        return user;
    }
}
